//
//  BrandManagementController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/12.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//


typedef NS_ENUM(NSInteger, BrandManagementVCCellType) {
    BrandManagementVCCellType_EditBrandName,//编辑品牌名
    BrandManagementVCCellType_EditBrandCreator,//编辑品牌创始人
};
#import "BrandManagementController.h"
#import "CommonTableViewCell.h"
#import "I_Brand.h"


@interface BrandManagementController ()
@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation BrandManagementController

#pragma mark Class Method
+ (instancetype)controller
{
    BrandManagementController *vc = [[BrandManagementController alloc] init];
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"健身房信息";
    [self configUI];
    _dataSource = @[@(BrandManagementVCCellType_EditBrandName),@(BrandManagementVCCellType_EditBrandCreator)];
    //查看品牌
    [self requesBrand];
}

#pragma mark UI
- (void)configUI
{
    [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonTableViewCell class])];
    self.tableView.insetTop = 10;
}

- (void)refreshUIWithBrand:(M_Brand *)brand
{
    if (brand) {
        M_Account *account = RI.account;
        account.brandName = brand.brandName;
        account.brandCreator = brand.brandCreator;
        RI.account = account;
        [self.tableView reloadData];
    }
}

#pragma mark NET

/**
 查看品牌
 */
- (void)requesBrand
{
    WeakSelf(self);
    [I_Brand getBrandOnSuccess:^(M_Brand *brand) {
        [weakself refreshUIWithBrand:brand];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonTableViewCell class])];
    cell.showStyle = CommonTableViewShowStyleArrow_RightText;
    BrandManagementVCCellType cellType = [_dataSource[indexPath.row] integerValue];
    switch (cellType) {
        case BrandManagementVCCellType_EditBrandName:
        {
            cell.leftLabel.text = @"健身房名称";
            cell.rightLabel.text = RI.account.brandName;
        }
            break;
        case BrandManagementVCCellType_EditBrandCreator:
        {
            cell.leftLabel.text = @"管理员名称";
            cell.rightLabel.text = RI.account.brandCreator;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BrandManagementVCCellType cellType = [_dataSource[indexPath.row] integerValue];
    switch (cellType) {
        case BrandManagementVCCellType_EditBrandName:
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_BrandName originStr:RI.account.brandName];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                [I_Brand editBrandWithParams:@{@"brandName":editStr,@"brandCreator":RI.account.brandCreator} success:^(M_Brand *brand) {
                    [weakself refreshUIWithBrand:brand];
                } failure:^(NSError *error) {
                    [AV showError:error];
                }];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case BrandManagementVCCellType_EditBrandCreator:
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_BrandCreator originStr:RI.account.brandCreator];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                [I_Brand editBrandWithParams:@{@"brandName":RI.account.brandName,@"brandCreator":editStr} success:^(M_Brand *brand) {
                    [weakself refreshUIWithBrand:brand];
                } failure:^(NSError *error) {
                    [AV showError:error];
                }];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
@end
